package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.AddressBook;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Api(tags = "C端购物车相关接口")
@RequestMapping("/user/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @ApiOperation("添加购物车")
    @PostMapping("/add")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("C端添加购物车数据:{}",shoppingCartDTO);
        shoppingCartService.add(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 返回当前用户的购物车数据
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("C端查看购物车")
    public Result<List<ShoppingCart>> getShorppingCartList () {
        log.info("C端查看购物车,用户ID:{}", BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartService.getCurrentUserShorppingCartList();
        return Result.success(list);
    }

    /**
     * 清空当前用户的购物车
     * @return
     */
    @DeleteMapping("/clean")
    @ApiOperation("C端清空当前用户的购物车")
    public Result clearShoppingCart () {
        Long currentId = BaseContext.getCurrentId();
        log.info("清空购物车数据,userId:{}", currentId);
        shoppingCartService.clearShoppingCart(currentId);
        return Result.success();
    }


    /**
     * 当前用户购物车指定菜品数量减一
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/sub")
    @ApiOperation("C端删除购物车中一个商品")
    public Result subFromShoppintCart (@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("删除购物车中的一个商品:{}",shoppingCartDTO);
        shoppingCartService.subFromShoppintCart(shoppingCartDTO);
        return Result.success();
    }

}
